//
//  THJPostZiZhiVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/12/22.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJPostZiZhiVC.h"
#import "ZBNetworking.h"
@interface THJPostZiZhiVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    NSInteger index;
    NSInteger imgFlag;
}
@end

@implementation THJPostZiZhiVC

- (void)viewDidLoad{
    [super viewDidLoad];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"上传资质"];
    }else{
        [self setNavBarWithTitle:@"上传资质"];
    }
   
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImg:)];
    
    self.imgV1.userInteractionEnabled = YES;
    
    [self.imgV1 addGestureRecognizer:tap];
    
    // Do any additional setup after loading the view from its nib.
}
-(void)getData{
    __weak typeof(self) weekself = self;
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=my_honor&r=my_honor&m=edit"];
    NSLog(@"zhengshuurl-%@",urlstr);
    NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    [MBProgressHUD showMessage:@"" toView:self.view];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=urlstr;
        request.apiType=ZBRequestTypeCache;
        [request setValue:token forHeaderField:@"token"];
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [MBProgressHUD hideHUDForView:weekself.view animated:YES];
        NSLog(@"用户中心-%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
        
        }
    } failed:^(NSError *error){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"error-%@",error);
    }];
}
-(void)selectImg:(UITapGestureRecognizer*)tap{
    index = tap.view.tag;
   
    [self alertWithTitle1:@"取消" title2:@"拍照" title3:@"相册"];
}
#pragma mark ----更多操作---
-(void)actionSheetIndex0Function{
    NSLog(@"取消1");
}
-(void)actionSheetIndex1Function{
    [self getPhotoWithType:0];
}
-(void)actionSheetIndex2Function{
    [self getPhotoWithType:1];
}

/**
 调用相册或相机
 */
-(void)getPhotoWithType:(NSInteger)index{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (index) {
            case 0:
                //来源:相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                //来源:相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.navigationBar.translucent = NO;
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.imgV1.image = image;
    imgFlag = 99;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)postBtnAction:(id)sender {
    if(!self.tf1.text.length){
        [self HUDShowMsg:@"请填写证书名称" AfterDelayDimiss:1];
        return;
    }else if (!self.tf2.text.length){
        [self HUDShowMsg:@"请填写发证机关" AfterDelayDimiss:1];
        return;
    }else if(imgFlag != 99){
        [self HUDShowMsg:@"请选择证书照片" AfterDelayDimiss:1];
        return;
    }
    NSString * urlStr = [NSString stringWithFormat:@"%@index.php?f=my_honor&r=my_honor&m=add",THJUrlDomain];
    NSDictionary * dic = @{@"title":self.tf1.text,
                           @"authority":self.tf2.text,
                           @"thumb":[self image2DataURL:self.imgV1.image]
                           };
    [THJNetWork postDataFromUrl:urlStr parameters:dic Token:[THJTools getToken] callback:^(id receiveData, NSError *error) {
        NSLog(@"receiveData-%@",receiveData);
        [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if([receiveData[@"status"] isEqual:@"200"]){
            [self performSelector:@selector(popSelf) withObject:nil afterDelay:1];
        }
    }];
}
-(void)popSelf{
    [self pop];
}
@end
